package org.deyang.tools.amazon.utils;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;

public class GlobalConstants {
	
    /*
     * Your AWS Access Key ID, as taken from the AWS Your Account page.
     */
	public static final String AWS_ACCESS_KEY_ID = "AKIAIAHIG7YUEG5T3SJQ";
	
	/*
     * Your AWS Secret Key corresponding to the above ID, as taken from the AWS
     * Your Account page.
     */
	public static final String AWS_SECRET_KEY = "t/wfJkvXEzfaDDYJ6aEvE64CwyplG/VYxuv61QBf";

    /*
     * Use one of the following end-points, according to the region you are
     * interested in:
     * 
     *      US: ecs.amazonaws.com 
     *      CA: ecs.amazonaws.ca 
     *      UK: ecs.amazonaws.co.uk 
     *      DE: ecs.amazonaws.de 
     *      FR: ecs.amazonaws.fr 
     *      JP: ecs.amazonaws.jp
     * 
     */
	public static final String ENDPOINT = "webservices.amazon.com";
	
	public static final Calendar KEEPASTARTDATE = new GregorianCalendar(2011, Calendar.JANUARY, 1, 0, 0, 0);
	
	public static final String TWEET_QUERY_PROPERTY_NAME = "tweetquerystring";
	
	public static final String ASSOCIATE_TAG = "doherty-20";

	public static final String SERVICE_NAME = "AWSECommerceService";
	
	public static final String KEEPA_URL_PREFIX = "http://dyn.keepa.com/21";
	
	// 一小时的毫秒数 60*60*1000
	public final static long HOUR_MILLIS = 3600000;
	
	private Entity configEntity = null;
	
	
	public synchronized Entity getGlobalConfiguration(){
		if (this.configEntity == null){
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			Query configq = new Query("GlobalConfiguration");
			Iterator<Entity> it = datastore.prepare(configq).asIterator();
			if (it.hasNext()){
				this.configEntity = it.next();
			}else{
				this.configEntity = new Entity("GlobalConfiguration");
				this.configEntity.setProperty(GlobalConstants.TWEET_QUERY_PROPERTY_NAME, "source:web kindle");
				datastore.put(configEntity);
			}
		}
		return this.configEntity;
		
	}
	
	public synchronized void saveGlobalConfiguration(Entity configEntity){
		this.configEntity = configEntity;
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(configEntity);
	}
	
	

}
